<template>
    <div>
        <h1>门店添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>门店名称</td>
                    <td><input type="text" v-model="info.name"></td>
                </tr>
                  <tr>
                    <td>分店名称</td>
                    <td><input type="text" v-model="info.oname"></td>
                </tr>
                 <tr>
                    <td>分店地址</td>
                    <td>
                           <select v-model="info.addid">
            <option value="">请选择</option>
            <option :value="item.aid" v-for="item in level" :key="item.aid">{{item.aname}}</option>  
        </select>
                    </td>
                </tr>
                   <tr>
                    <td>图片</td>
                    <td><input type="file" @change="img">
                        <img :src="info.img" width="50px" height="50px">
                    </td>
                </tr>
                  <tr>
                    <td>客服电话</td>
                    <td><input type="text" v-model="info.tel"></td>
                </tr>
                  <tr>
                    <td>门店状态</td>
                    <td><input type="radio" value="1" name="aa" v-model="info.state">启用
                        <input type="radio" value="0" name="aa" v-model="info.state">停用
                    </td>
                </tr>
                <tr>
                    <td>门店介绍</td>
                    <td><textarea v-model="info.introduce"></textarea></td>
                </tr>
                 <tr>
                    <td>详细地址</td>
                    <td><textarea v-model="info.add"></textarea></td>
                </tr>
                  <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
        {{info}}
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref} from 'vue'
import { onMounted } from 'vue';
onMounted(()=>{
    getlevel();
})
let level:any=ref([]);

let info:any=ref({
     "id": 0,
  "name": "",
  "oname": "",
  "img": "",
  "tel": "",
  "state": '',
  "introduce": "",
  "add": "",
  "addid": ""
})

const getlevel=()=>{
    axios({
        url:'https://localhost:7058/api/Pro/proLevels',
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        level.value=res.data
    })
    .catch((err)=>{
        console.log(err);
    })
}

const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7058/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.img=res.data
    })
}
const add=()=>{
    if(info.value.name==""){
        alert("门店名称不能为空");
        return;
    }
    if(info.value.oname==""){
        alert("分店名称不能为空");
        return;
    }
    axios({
        url:'https://localhost:7058/api/Pro/AddPro',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("门店已存在")
            return;
        }
        alert("添加成功");
    })
    .catch((err)=>{
        console.log(err);
    })
}

</script>

<style scoped>

</style>